import pandas as pd

# 创建一个示例DataFrame
prices = pd.DataFrame(data={'close': [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]},
                      index=pd.date_range(start='2022-02-01', periods=12, freq='D'))

# 根据日期将DataFrame中的数据按照上中下旬分组
groups = pd.cut(prices.index.day, bins=[0, 10, 20, 31], labels=['first_10_days', 'second_10_days', 'last_10_days'])

# 将分组结果添加到DataFrame中
prices['group'] = groups

# 对DataFrame进行分组并仅输出每组的前3个交易日收盘价
grouped_prices = prices.groupby('group')['close'].apply(lambda x: x.head(3))
print(grouped_prices)
